package com.mini.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.mini.common.EncryptField;
import com.mini.constant.OrderChannelType;
import com.mini.es.model.EsBusinessStudentBigLessonXcx;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Builder;
import lombok.Data;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.Objects;


@Data
@ToString
@ApiModel(value = "大课订单列表的对象")
@Builder
public class BigLessonOrderPageVO {


    @ApiModelProperty(value = "[大课]大课订单号")
    private String boOrderNo;
    @ApiModelProperty(value = "[大课]大课订单手机号")
    @EncryptField
    private String boPhoneNo;
    @ApiModelProperty(value = "[大课]大课商品id")
    private Long boLessonId;
    @ApiModelProperty(value = "[大课]大课商品名称")
    private String boLessonName;
    @ApiModelProperty(value = "[大课]大课成单渠道：1-直播带货，0-有赞")
    private Integer boOrderSourceType;
    @ApiModelProperty(value = "[大课]大课成单时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date boOrderPayFinishTime;
    @ApiModelProperty(value = "[大课]大课商品价格")
    private Float boPrice;
    @ApiModelProperty(value = "[大课]大课实付金额")
    private Float boPayAmount;
    @ApiModelProperty(value = "[大课]订单状态code")
    private String boOrderState;
    @ApiModelProperty(value = "[大课]订单状态name")
    private String boOrderStateStr;
    @ApiModelProperty(value = "[大课]备注")
    private String boOperateRemark;

    private String boCouponCode;
    @ApiModelProperty(value = "[大课]定金尾款类型：0-整单，1-定金，2-尾款")
    private Integer boOrderDepositType;
    @ApiModelProperty(value = "[大课]关联字段，定义哪些是一套订单")
    private String boRelatedKey;

    @ApiModelProperty(value = "[大课][定金]定金订单号")
    private String boDepositOrderNo;
    @ApiModelProperty(value = "[大课][定金]定金与尾款的绑定状态：1-绑定，0-未绑定")
    private Integer boDepositMergeState;
    @ApiModelProperty(value = "[大课][定金]定金支付时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date boDepositPayTime;
    private Long boDepositLessonId;
    @ApiModelProperty(value = "[大课][定金]定金名称")
    private String boDepositLessonName;
    @ApiModelProperty(value = "[大课][定金]定金支付金额")
    private Float boDepositPayAmount;
    @ApiModelProperty(value = "[大课]业务类型code")
    private Integer boOrderChannelType;
    @ApiModelProperty(value = "[大课]业务类型")
    private String boOrderChannelTypeName;


    @ApiModelProperty(value = "[20210422][大课]退费时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date boRefundTime;
    @ApiModelProperty(value = "[20210422][大课]退费状态")
    private String boRefundStatus;
    @ApiModelProperty(value = "[20210422][大课]退费金额")
    private Double boRefundAmount;

    @ApiModelProperty(value = "[20210422][大课]升班/转班状态code")
    private String boChangeClassStatus;
    @ApiModelProperty(value = "[20210422][大课]升班/转班状态Name")
    private String boChangeClassStatusName;
    @ApiModelProperty(value = "[20210422][大课]升班/转班状态关联订单号")
    private String boChangeClassRelatedKey;
    @ApiModelProperty(value = "[20210422][大课]升班/转班状态时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private String boChangeClassTime;

    @ApiModelProperty(value = "[小课]小课订单关联类型（小课订单手机号栏位使用）：0-无匹配小课+无设置归属，1-系统匹配，2-人工匹配，3-无匹配小课，设置了归属")
    private Integer moRelateFlag;

    @ApiModelProperty(value = "[小课]订单编号")
    private String moOrderNo;

    @ApiModelProperty(value = "[小课]支付手机号")
    @EncryptField
    private String moPhoneNo;

    @ApiModelProperty(value = "[小课]小课业务ID")
    private Long moMiniLessonId;

    @ApiModelProperty(value = "[小课]业务线，0加盐业务线 1集团业务线")
    private Integer moBusinessType;

    @ApiModelProperty(value = "[小课]小课订单支付时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date moOrderPayTime;

    @ApiModelProperty(value = "[小课]小课商品id")
    private String moLessonId;
    @ApiModelProperty(value = "[小课]小课商品名称")
    private String moLessonTitle;


    @ApiModelProperty(value = "[小课]微信备注昵称")
    private String moWechatNameNote;
    @ApiModelProperty(value = "[小课]微信昵称")
    private String moWechatName;

    @ApiModelProperty(value = "[小课]微信号")
    private String moWxId;
    @ApiModelProperty(value = "[小课]学员id")
    private String moStudentId;
    @ApiModelProperty(value = "[小课]姓名")
    private String moNickName;
    @ApiModelProperty(value = "[小课]班次信息")
    private String moClassName;

    @ApiModelProperty(value = "[小课]运营专员id")
    private Long yyTeacherId;

    @ApiModelProperty(value = "[小课]运营专员名称")
    private String yyTeacherName;

    @ApiModelProperty(value = "[小课]运营老师email")
    private String yyTeacherEmail;

    @ApiModelProperty(value = "[小课]运营老师nickname")
    private String yyTeacherUsername;


    @ApiModelProperty(value = "[小课]渠道code")
    private String channelCode;
    @ApiModelProperty(value = "[小课]渠道name")
    private String channelName;

    @ApiModelProperty(value = "[20201203][大课]大课skuCode")
    private String skuCode;

    @ApiModelProperty(value = "[20201203][大课]大课sku名称")
    private String skuName;


    @ApiModelProperty(value = "[20201203][小课]事业部id")
    private Long yyBuId;
    @ApiModelProperty(value = "[20201203][小课]事业部")
    private String yyBuName;

    @ApiModelProperty(value = "[20201203][小课]SKU架构id")
    private Long yySkuOrgId;
    @ApiModelProperty(value = "[20201203][小课]SKU架构")
    private String yySkuOrgName;


//    @ApiModelProperty(value = "[小课]运营组长id")
//    private Long yyGroupLeaderId;
//    @ApiModelProperty(value = "[小课]运营组长")
//    private String yyGroupLeaderName;
    @ApiModelProperty(value = "[小课]运营组id")
    private Long yyGroupId;
    @ApiModelProperty(value = "[小课]运营组")
    private String yyGroupName;
//
//    @ApiModelProperty(value = "[小课]运营部长id")
//    private Long yyDeptLeaderId;
//    @ApiModelProperty(value = "[小课]运营部长")
//    private String yyDeptLeaderName;
    @ApiModelProperty(value = "[小课]运营部id")
    private Long yyDeptId;
    @ApiModelProperty(value = "[小课]运营部")
    private String yyDeptName;
//
//    @ApiModelProperty(value = "[小课]工作室老师id")
//    private Long yyStudioLeaderId;
//    @ApiModelProperty(value = "[小课]工作室老师")
//    private String yyStudioLeaderName;
    @ApiModelProperty(value = "[小课]老师工作室id")
    private Long yyStudioId;
    @ApiModelProperty(value = "[小课]老师工作室")
    private String yyStudioName;

    @ApiModelProperty(value = "[小课]投放工作室id")
    private Long tfStudioId;
    @ApiModelProperty(value = "[小课]投放工作室")
    private String tfStudioName;

    @ApiModelProperty(value = "[小课]投放人员id")
    private Long tfFzId;
    @ApiModelProperty(value = "[小课]投放人员")
    private String tfFzName;


    @ApiModelProperty(value = "[20201104][二阶]机会id")
    private Long soChanceId;
    @ApiModelProperty(value = "[20201104][二阶]关联状态")
    private Integer soChanceStates;
    @ApiModelProperty(value = "[20201104][二阶]关联状态显示内容")
    private String soChanceStateStr;
    @ApiModelProperty(value = "[20201104][二阶]机会状态：0-无归属，1-一阶，2-二阶")
    private Integer soOperationStage;
    @ApiModelProperty(value = "[20201104][二阶]流水归属")
    private String soOrderBelong;
    @ApiModelProperty(value = "[20201104][二阶]二阶运营老师id")
    private Long soTeacherId;
    @ApiModelProperty(value = "[20201104][二阶]二阶运营老师")
    private String soTeacherName;
    @ApiModelProperty(value = "[20201214]续报类型：二阶类型：1-续报专用，2-流量复用，3-拉新裂变（转介绍）")
    private Integer soContinueType;



    public String getSoOrderBelong() {
        if(soOperationStage != null){
            if(soOperationStage == 1){
                soOrderBelong = "一阶【" + this.yyTeacherName + "】";
            }else if(soOperationStage == 2){
                soOrderBelong = "二阶【" + this.soTeacherName + "】";
            }
        }

        return soOrderBelong;
    }

    public String getSoChanceStateStr() {

        if(this.soChanceId != null){
            if(soChanceStates == null){

                soChanceStateStr = "订单取消";

            }else if(soChanceStates == 0){
                soChanceStateStr = "正常";
            }else if(soChanceStates == 1){
                soChanceStateStr = "订单取消";
            }else if(soChanceStates == 2){
                soChanceStateStr = "未关联（关联取消）";
            }else if(soChanceStates == 3){
                soChanceStateStr = "未关联（续报取消）";
            }
        }else {
            this.soChanceStateStr = "未关联";
        }

        return soChanceStateStr;
    }

    public static String getBoOrderChannelTypeName(Integer boOrderChannelType) {
        return OrderChannelType.getTypeName(boOrderChannelType);
    }

    public static BigLessonOrderPageVO from(EsBusinessStudentBigLessonXcx xcx){

        int moRelateFlag = 0;
        if((xcx.getOperationStage().intValue() != 0 && xcx.getBindState().intValue() == 0)){
            moRelateFlag = 1;
        }else if(xcx.getOperationStage().intValue() != 0 && xcx.getBindState().intValue() == 1){
            moRelateFlag = 2;
        }else if(xcx.getOperationStage().intValue() == 0 && (StringUtils.isEmpty(xcx.getSku()) || xcx.getSku().equals("0"))){
            moRelateFlag = 3;
        }

        if(Objects.nonNull(xcx.getChangeClassStatus()) && xcx.getChangeClassStatus().equals("0")){
            xcx.setChangeClassStatus(null);
            xcx.setChangeClassStatusName(null);
        }
        BigLessonOrderPageVO bigLessonOrderPageVO = BigLessonOrderPageVO.builder()
                .boOrderNo(xcx.getOrderNo())
                .boPhoneNo(xcx.getPhoneNumber())
                .boLessonId(xcx.getLessonId())
                .boLessonName(xcx.getLessonName())
                .boOrderSourceType(Integer.parseInt(xcx.getOrderSourceType()))
                .boOrderPayFinishTime(xcx.getPayFinishTime())
                .boPrice(xcx.getPrice())
                .boPayAmount(xcx.getPayAmount())
                .boOrderState(xcx.getOrderState())
                .boOrderStateStr(xcx.getOrderStateStr())
                .boOperateRemark(xcx.getOperateRemark())
                .boCouponCode(xcx.getCouponCode())
                .boOrderDepositType(xcx.getOrderDepositType())
                .boRelatedKey(xcx.getRelatedKey())

                .boDepositMergeState(xcx.getDepositMergeState())

                .moRelateFlag(moRelateFlag)
                .moOrderNo(xcx.getMiniOrderNo())
                .moPhoneNo(xcx.getPhoneNo())
                .moMiniLessonId(xcx.getMiniLessonId()==null?0L:Long.parseLong(xcx.getMiniLessonId()))
                .moBusinessType(xcx.getBusinessType())
                .moOrderPayTime(xcx.getOrderPayTime())
                .moLessonId(xcx.getOrderLessonId())
                .moLessonTitle(xcx.getLessonTitle())
                .moWechatNameNote(xcx.getWechatNameNote())
                .moWechatName(xcx.getWechatName())
                .moWxId(xcx.getWxId())
                .moStudentId(xcx.getUserId())
                .moNickName(xcx.getNickName())
                .yyTeacherId((xcx.getMiniTeacherId()==null?xcx.getTeacherId().longValue():xcx.getMiniTeacherId().longValue()))
//                .yyTeacherName(xcx.getTeacherName().toString())
                .channelCode(xcx.getChannelCode())
                .channelName(xcx.getChannelName())
                .skuCode(xcx.getSkuCode())
                .skuName(xcx.getSkuName())
//                .moClassName()
//                .yyTeacherEmail()
//                .yyTeacherUsername()
//                .boDepositOrderNo()
//                .boDepositPayTime()
//                .boDepositLessonId()
//                .boDepositLessonName()
//                .boDepositPayAmount()
//                .boOrderChannelType()
//                .boOrderChannelTypeName()
//                .yyBuId()
//                .yyBuName()
//                .yySkuOrgId()
//                .yySkuOrgName()
//                .yyGroupId(xcx.getGroupId())
//                .yyGroupName()
//                .yyDeptName()
//                .soChanceStateStr()
//                .soOrderBelong()
                .yyDeptId(Long.parseLong(xcx.getOd()))
                .yyStudioId(Long.parseLong(xcx.getWorkRoom()))
                .yyStudioName(xcx.getWorkRoomName())
                .tfStudioId(xcx.getWorkRoomId()==null?null:xcx.getWorkRoomId().longValue())
                .tfStudioName(xcx.getWorkRoomName())
                .tfFzId(xcx.getFzId()==null?0L:xcx.getFzId().longValue())
                .tfFzName(xcx.getFzName())
                .soChanceId(xcx.getSecondOrderId())
                .soChanceStates(xcx.getChanceStates())
                .soOperationStage(xcx.getOperationStage())
                .soTeacherId(xcx.getSecondTeacherId())
                .soTeacherName(xcx.getSecondTeacherName())
                .soContinueType(xcx.getContinueType())
                .boOrderChannelType(xcx.getOrderChannelType())
                .boOrderChannelTypeName(getBoOrderChannelTypeName(xcx.getOrderChannelType()))
                //新增
                .boRefundAmount(xcx.getRefundAmount()==null?null:Double.valueOf(xcx.getRefundAmount()))
                .boRefundStatus(xcx.getRefundStatus())
                .boRefundTime(xcx.getRefundTime())
                .boChangeClassStatus(xcx.getChangeClassStatus())
                .boChangeClassStatusName(xcx.getChangeClassStatusName())
                .boChangeClassRelatedKey(xcx.getChangeClassRelatedKey())
                .build();

        return bigLessonOrderPageVO;

    }
}